var allEditors={},LANGUAGES={"":"text/x-c++src","lang-bsh":"text/x-sh","lang-c":"text/x-csrc","lang-cc":"text/x-csrc","lang-cpp":"text/x-c++src","lang-cs":"text/x-c++src","lang-csh":"text/x-c++src","lang-cyc":"text/x-c++src","lang-cv":"text/x-c++src","lang-htm":"text/html","lang-html":"text/html","lang-java":"text/x-java","lang-js":"text/javascript","lang-m":"text/html","lang-mxml":"text/html","lang-perl":"text/x-perl","lang-pl":"text/x-sh","lang-pm":"text/x-sh","lang-py":"text/x-python","lang-rb":"text/x-ruby",
"lang-sh":"text/x-sh","lang-xhtml":"htmlmixed","lang-xml":"text/html","lang-xsl":"text/html"};function escapeRegExp(a){return a.replace(/([.*+?^=!:${}()|\[\]\/\\])/g,"\\$1")}function replaceAll(a,c,b){return a.replace(new RegExp(escapeRegExp(c),"g"),b)}
var EMarkdownEditor=function(a,c,b){c=void 0===c?"":c;b=void 0===b?"":b;var d=this;this.fullscreen=!1;this.id=a;this.widget=jQuery("#"+a+"_markdown_widget");this.textarea=jQuery("#"+a);this.tabPreviewLink=jQuery("#"+a+"_tab_preview_link");this.tabPreviewLink.bind("shown.bs.tab",{widgetId:a},EMarkdownEditor.updatePreview);this.fullScreenButton=jQuery("#"+a+"_fullscreen_btn");this.fullScreenButton.on("click",(a)=>{a.preventDefault();d.fullScreen()});this.linkDialog=jQuery("#"+a+"_add_link_dialog");
this.addLinkBtn=jQuery("#"+a+"_add_link_btn");this.addLinkBtn.bind("click",{dialog:this.linkDialog},EMarkdownEditor.showDialog);this.insertLinkBtn=jQuery("#"+a+"_insert_link_btn");this.insertLinkBtn.bind("click",{widgetId:a},EMarkdownEditor.insertLink);this.linkText=jQuery("#"+a+"_link_text");this.linkUrl=jQuery("#"+a+"_link_url");this.codeDialog=jQuery("#"+a+"_code_dialog");this.addCodeBtn=jQuery("#"+a+"_add_code_btn");this.addCodeBtn.bind("click",{dialog:this.codeDialog},EMarkdownEditor.showDialog);
this.insertCodeBtn=jQuery("#"+a+"_insert_code_btn");this.insertCodeBtn.bind("click",{widgetId:a},EMarkdownEditor.insertCode);this.selectCode=jQuery("#"+a+"_select_code");this.selectCode.bind("change",{widgetId:a},EMarkdownEditor.onSelectCode);this.codeInput=jQuery("#"+a+"_code_input");this.uploadLink=c;this.addImageBtn=jQuery("#"+a+"_add_image_btn");this.addImageBtn.bind("click",{widgetId:a},EMarkdownEditor.showUploadDialog);this.uploadFileLink=b;this.addFileBtn=jQuery("#"+a+"_add_file_btn");this.addFileBtn.bind("click",
{widgetId:a},EMarkdownEditor.showUploadFileDialog);this.addCutBtn=jQuery("#"+a+"_add_cut_btn");this.addCutBtn.bind("click",{widgetId:a},EMarkdownEditor.insertCut);a=replaceAll(a,"-","_");try{this.mirrorEditor=eval(a+"_code_input_codemirror"),this.mirrorEditor.on("change",(a)=>{d.codeInput.val(a.getValue())}),this.mirrorEditor.setOption("extraKeys",{"Ctrl-/":"toggleComment"})}catch(f){console.log("Code editor not found. id:",a+"_code_input_codemirror")}try{this.markdownMirrorEditor=eval(a+"_codemirror"),
this.markdownMirrorEditor.on("change",(a)=>{d.textarea.val(a.getValue())}),this.markdownMirrorEditor.setOption("extraKeys",{F11(a){d.fullScreen()},Esc(a){jQuery("body").removeClass("overflow-hidden");d.widget.removeClass("markdown-fullscreen");d.fullscreen=!1;d.fullScreenButton.find("span").removeClass("mdi-fullscreen-exit");d.fullScreenButton.find("span").addClass("mdi-fullscreen")},"Ctrl-/":"toggleComment"})}catch(e){console.log("Markdown editor not found. id:",a+"_codemirror")}};
EMarkdownEditor.prototype.fullScreen=function(){this.fullscreen?(jQuery("body").removeClass("overflow-hidden"),this.widget.removeClass("markdown-fullscreen"),this.fullscreen=!1,this.fullScreenButton.find("span").removeClass("mdi-fullscreen-exit"),this.fullScreenButton.find("span").addClass("mdi-fullscreen")):(jQuery("body").addClass("overflow-hidden"),this.widget.addClass("markdown-fullscreen"),this.fullscreen=!0,this.fullScreenButton.find("span").removeClass("mdi-fullscreen"),this.fullScreenButton.find("span").addClass("mdi-fullscreen-exit"))};
EMarkdownEditor.onSelectCode=(a)=>{a=EMarkdownEditor.get(a.data.widgetId);a.mirrorEditor.setOption("mode",LANGUAGES[a.selectCode.val()])};EMarkdownEditor.insertCut=(a)=>{a.preventDefault();(a=EMarkdownEditor.get(a.data.widgetId))&&a.markdownMirrorEditor.replaceSelection("\n___\n");return!1};
EMarkdownEditor.showUploadDialog=(a)=>{a.preventDefault();var c=a.data.widgetId,b=EMarkdownEditor.get(c);if(b.uploadLink.length){var d=jQuery("#uploadDialog");d.length?(EMarkdownEditor.initUploadDialog(c),d.modal("show")):jQuery.ajax({url:b.uploadLink,type:"GET",dataType:"json",success(b){jQuery(b.uploadDialog).appendTo(jQuery("body"));EMarkdownEditor.showUploadDialog(a)}})}};
EMarkdownEditor.initUploadDialog=(a)=>{var c=EMarkdownEditor.get(a),b=jQuery("#uploadDialog");b.find("#id_file").change(function(){if(this.files&&this.files[0]){var a=new FileReader;a.onload=(a)=>{var d=jQuery("#upload_image");d.on("load",(a)=>{var d=new Cropper(upload_image,{viewMode:1});b.find("#js-zoom-in").click(()=>{d.zoom(.1)});b.find("#js-zoom-out").click(()=>{d.zoom(-.1)});b.on("hidden.bs.modal",()=>{d.destroy()});b.find("#js-crop-and-upload").click((a)=>{a.preventDefault();var e=new FormData(b.find("#formUpload").get(0));
e&&d.getCroppedCanvas().toBlob((a)=>{e.set("file",a,"photo.jpg");jQuery.ajax({url:c.uploadLink,type:"POST",data:e,cache:!1,processData:!1,contentType:!1,success(a){a.result&&(c.markdownMirrorEditor.replaceSelection("\n[!["+a.description+"]("+a.src+")]("+a.url+")\n"),b.find("#upload-size-warning").addClass("d-none"),b.modal("hide"))},error(a,c,d){413===a.status&&b.find("#upload-size-warning").removeClass("d-none")}})});return!1});b.find("#upload-size-warning").addClass("d-none");b.find("#cropBody").removeClass("d-none");
b.find("#cropFooter").removeClass("d-none");b.find("#file_form_group").addClass("d-none");b.find("#id_content").parent().removeClass("d-none")});d.attr("src",a.target.result)};a.readAsDataURL(this.files[0])}});b.on("hidden.bs.modal",()=>{b.remove()})};
EMarkdownEditor.initUploadFileDialog=(a)=>{var c=EMarkdownEditor.get(a),b=jQuery("#uploadFileDialog");b.find("#id_file").change(function(a){this.files&&this.files[0]&&(b.find("#file-upload-submit").click((a)=>{a.preventDefault();(a=new FormData(b.find("#formUpload").get(0)))&&jQuery.ajax({url:c.uploadFileLink,type:"POST",data:a,cache:!1,processData:!1,contentType:!1,success(a){a.result&&(c.markdownMirrorEditor.replaceSelection("\n[!["+a.name+"](/static/images/file.svg) "+a.name+"]("+a.url+")\n"),
b.find("#upload-size-warning").addClass("d-none"),b.modal("hide"))},error(a,c,d){413===a.status&&b.find("#upload-size-warning").removeClass("d-none")}});return!1}),b.find("#file_info").removeClass("d-none"),b.find("#file_name").html(a.target.files[0].name),b.find("#file_form_group").addClass("d-none"),b.find("#id_content").parent().removeClass("d-none"),b.find("#uploadFooter").removeClass("d-none"))});b.on("hidden.bs.modal",()=>{b.remove()})};
EMarkdownEditor.showUploadFileDialog=(a)=>{a.preventDefault();var c=a.data.widgetId,b=EMarkdownEditor.get(c);if(b.uploadFileLink.length){var d=jQuery("#uploadFileDialog");d.length?(EMarkdownEditor.initUploadFileDialog(c),d.modal("show")):jQuery.ajax({url:b.uploadFileLink,type:"GET",dataType:"json",success(b){jQuery(b.uploadDialog).appendTo(jQuery("body"));EMarkdownEditor.showUploadFileDialog(a)}})}};
EMarkdownEditor.insertCode=(a)=>{a.preventDefault();if(a=EMarkdownEditor.get(a.data.widgetId)){var c=a.codeInput.val(),b=a.selectCode.val();if(0<c.length)return a.markdownMirrorEditor.replaceSelection("\n```"+b+"\n"+c+"\n```\n"),a.codeInput.val(""),a.mirrorEditor.getDoc().setValue(""),!0}return!1};
EMarkdownEditor.insertLink=(a)=>{a.preventDefault();if(a=EMarkdownEditor.get(a.data.widgetId)){var c=a.linkUrl.val();if(0<c.length){var b=a.linkText.val(),d=void 0,d=0<b.length?"["+b+"]("+c+")":"["+c+"]("+c+")";a.markdownMirrorEditor.replaceSelection(d);a.linkUrl.val("");a.linkText.val("");return!0}}return!1};EMarkdownEditor.showDialog=(a)=>{a.data.dialog.modal("show");a.data.dialog.css("z-index",2E3);return!1};
EMarkdownEditor.create=(a,c,b)=>{c=new EMarkdownEditor(a,void 0===c?"":c,b);return allEditors[a]=c};EMarkdownEditor.remove=(a)=>{delete allEditors[a]};EMarkdownEditor.get=(a)=>allEditors[a];EMarkdownEditor.saveSelection=()=>{if(window.getSelection){var a=window.getSelection();if(a.getRangeAt&&a.rangeCount)return a.getRangeAt(0)}else if(document.selection&&document.selection.createRange)return document.selection.createRange();return null};
EMarkdownEditor.updatePreview=(a)=>{var c=a.data.widgetId;$.ajax({url:"/evileg_core/markdown/",type:"POST",data:{content:jQuery("#"+c).val()},dataType:"json",success(a){jQuery("#"+c+"_preview").html(a.preview);prettyPrint()}})};
